/*****************************************************************************
 * Copyright (c) 2019, Nations Technologies Inc.
 *
 * All rights reserved.
 * ****************************************************************************
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the disclaimer below.
 *
 * Nations' name may not be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
 * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * ****************************************************************************/

/**
 * @file main.c
 * @author Nations
 * @version v1.0.0
 *
 * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
 */
#include "n32g031.h"
#include "stdio.h"
#include "main.h"

#include "DevicesCRC.h"
#include "DevicesDelay.h"
#include "DevicesTimer.h"
#include "DevicesQueue.h"
#include "DevicesWatchDog.h"
#include "DevicesUart.h"
#include "DevicesLed.h"
#include "DevicesKey.h"
#include "DevicesBuzz.h"
#include "DevicesBlower.h"
#include "DevicesInfrared.h"
#include "DevicesPower.h"
#include "DevicesSoftIIC.h"
#include "DevicesADC.h"
#include "DevicesSoftTimer.h"
#include "DevicesInput.h"
#include "radio.h"
#include "qmp6989.h"


void vUserSystemInit(void)
{
    cTimer3Init();
    cTimer6Init();
    vDelayInit();

    vHardCRCInit();

    vPowerInit();

    enumQueueInit();

    vLedInit();

    vKeyInit();

    vInputInit();

    vBuzzInit();

    vBlowerACInit();

    vInfraredInit();

    vADCInit();

    vSoftIIC6998Init();
    qmp6989_initialization();

    RF_Init();

    vWatchdogInit();
}



KeyTypeDef typeKeyData = {0};
SoftTimerTypeDef typeSoftTimerPrintf = {0};
SoftTimerTypeDef typeSoftTimerBlower = {0};
float st_fADCDatas[ADC1_SAMPLING_CHANNEL] = {0};
float fQMP6989Pressure = 0.0f, fQMP6989Temperature = 0.0f, fPTCTemperature = 0.0f;

/**
 * @brief  Main program
 */
int main(void)
{
    int32_t i = 0;

    SystemCoreClockUpdate();

    /* AFIO clock enable */
    RCC_EnableAPB2PeriphClk(RCC_APB2_PERIPH_AFIO , ENABLE);

    vUserSystemInit();


    cSoftTimerSetMs(&typeSoftTimerPrintf, 1000, softTimerOpen);
    cSoftTimerSetS(&typeSoftTimerBlower,  10, softTimerOpen);

    while (1)
    {
        /* 按键输入 */
        while(enumQueuePopDatas(&g_TypeQueueKeyInput, (uint8_t *)&typeKeyData, sizeof(KeyTypeDef)) == queueNormal)
        {
            /* 短按并已经松开 */
            if(typeKeyData.state == (keyShort | keyCut))
            {
                /* 风干按键 */
                if(typeKeyData.valueLoosen == KEY_AIR_DRYING)
                    vPowerClose(POWER_AIR_DRYING | POWER_PTC);
                /* 集尘按键 */
                else if(typeKeyData.valueLoosen == KEY_DUST_HOOD)
                    vBlowerACSpeedSet(0.0f);
                /* 风干 + 集尘组合按键 */
                else if(typeKeyData.valueLoosen == (KEY_AIR_DRYING | KEY_DUST_HOOD))
                    printf("KEY_AIR_DRYING | KEY_DUST_HOOD keyShort | keyCut.\r\n");
            }
            /* 长按并还未松开 */
            else if(typeKeyData.state == keyLong)
            {
                /* 风干按键 */
                if(typeKeyData.valuePress == KEY_AIR_DRYING)
                    vPowerOpen(POWER_AIR_DRYING | POWER_PTC);
                /* 集尘按键 */
                else if(typeKeyData.valuePress == KEY_DUST_HOOD)
                    vBlowerACSpeedSet(0.2f);
                /* 风干 + 集尘组合按键 */
                else if(typeKeyData.valuePress == (KEY_AIR_DRYING | KEY_DUST_HOOD))
                    printf("KEY_AIR_DRYING | KEY_DUST_HOOD keyLong.\r\n");

                /* 重新开始计时 */
                cSoftTimerReset(&typeSoftTimerBlower);
            }

            vBuzzSetStatus(BUZZ_DISABLE, 1);
        }

        /* 到期关闭风机，防止一直工作损坏风机 */
        if(enumSoftTimerGetState(&typeSoftTimerBlower) == softTimerOver)
        {
            cSoftTimerReset(&typeSoftTimerBlower);

            vBlowerACSpeedSet(0.0f);
            vPowerClose(POWER_AIR_DRYING | POWER_PTC);
        }

        /* 到期执行 */
        if(enumSoftTimerGetState(&typeSoftTimerPrintf) == softTimerOver)
        {
            cSoftTimerReload(&typeSoftTimerPrintf);

            printf("Hello ARAY!.\r\n");

            for(i = 0; i < ADC1_SAMPLING_CHANNEL; ++i)
            {
                st_fADCDatas[i] = fADCGetValue(i);
                printf("ADC %d: %.2f\r\n", i, st_fADCDatas[i]);
            }

            vQMP6989ValueGet(&fQMP6989Pressure, &fQMP6989Temperature);
            printf("fQMP6989Pressure: %.2f, fQMP6989Temperature: %.2f\r\n", fQMP6989Pressure, fQMP6989Temperature);

            fPTCTemperature = fBlowerACTemperatureGet();
            printf("fPTCTemperature: %.2f\r\n", fPTCTemperature);
        }


        /* 喂狗 */
        vWatchdogReload();
    }
}

int fputc(int ch, FILE *f)
{
    // /* Transmit Data */
    // // while((RESET == USART_GetFlagStatus(USART2, USART_FLAG_TXDE)) && (USART2->CTRL1 & CTRL1_UEN_SET));
    // while((RESET == USART_GetFlagStatus(USART2, USART_FLAG_TXDE)) && (USART2->CTRL1 & 0x2000));
    // USART2->DAT = ch;

    return ch;
}

#ifdef USE_FULL_ASSERT

/**
 * @brief  Reports the name of the source file and the source line number
 *         where the assert_param error has occurred.
 * @param file pointer to the source file name
 * @param line assert_param error line source number
 */
void assert_failed(const uint8_t* expr, const uint8_t* file, uint32_t line)
{
    /* User can add his own implementation to report the file name and line number,
     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

    while (1)
    {
    }
}

#endif

/**
 * @}
 */

/**
 * @}
 */
